#!/bin/sh
WAP_INFO_FELE="/mnt/nand1-2/update/Software.info"
UPDATE_HTTP_ADDR=`awk '{if ($1=="HTTP_ADDR") {print $2}}' $WAP_INFO_FELE`
WAPDATA_ETAG=`awk '{if ($1=="ETag:") {print $2}}' $WAP_INFO_FELE`
if [ "$WAPDATA_ETAG" == "" ]; then
  echo "$WAP_INFO_FELE is bad file"
  cp /mnt/nand1-2/update/Software.sw /mnt/nand1-2/update/Software.info
  exit 1;
  fi
cd /mnt/nand1-2/update
echo "HTTP_ADDR:$UPDATE_HTTP_ADDR"
/mnt/nand1-1/bin/curl -I "$UPDATE_HTTP_ADDR" --speed-time 5 --speed-limit 1
echo "curl Etag result $?"
if [ $? -eq 0 ];then
  Etag=`/mnt/nand1-1/bin/curl -I "$UPDATE_HTTP_ADDR" --speed-time 5 --speed-limit 1`
  echo "$Etag"
  EtagFlag=`echo "$Etag" | grep ETag:.`	
  Etag=`echo "$EtagFlag" | awk '{print $2}'`
  if [ -z "$EtagFlag" ]; then		
    echo "net Etag can't is NULL"
    exit 0
  fi
  echo "Sever : $Etag"
  echo "System: $WAPDATA_ETAG $UPDATE_HTTP_ADDR"
  if [ "$Etag" == "$WAPDATA_ETAG" ]; then	
    echo "This APP is New"
  else	./update.sh	
    echo "update result $?"
  if [ $? -eq 0 ];then
    echo "update Etag:$EtagFlag"
    sed -i "3c $EtagFlag" $WAP_INFO_FELE 	
    sync
    sleep 1
    echo "Update Software.info File :"
    cat "$WAP_INFO_FELE"
    echo "system update finsh ..............................."
    reboot	
  else
    echo "Update file fail ........................."
    /mnt/nand1-1/sd700&
    /mnt/nand1-1/monitoring.sh&	
  fi
  fi
fi
